package nix.dao.impl;

import java.util.List;

import nix.dao.UserDao;
import nix.domain.User;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

public class UserDaoImpl extends BaseDaoImpl<User> implements UserDao {
	
	@Override
	public User GetByUserCode(final String user_code) {

		// HQL
		/*
		return getHibernateTemplate().execute(new HibernateCallback<User>() {

			@Override
			public User doInHibernate(Session session) throws HibernateException {
				String hql = "from nix.domain.User where user_code = :usercode";
				Query query = session.createQuery(hql);
				query.setParameter("usercode", user_code);
				User u = (User) query.uniqueResult();
				return u;
			}
		});
		*/
		
		// Criteria
		DetachedCriteria dc = DetachedCriteria.forClass(User.class);
		dc.add(Restrictions.eq("user_code", user_code));
		List<User> list = (List<User>) getHibernateTemplate().findByCriteria(dc);
		if (list != null && list.size() > 0) {
			return list.get(0);
		} else {
			return null;
		}
	}

}
